using UnityEngine;
using System.Collections;

public class move : MonoBehaviour {
	
	
	public GameObject larm;
	public GameObject rarm;

	public GameObject frontRay;
	public GameObject backRay;
	
	public AudioClip jump;
	public AudioClip landing;
	public AudioClip footstep;
	public GameObject stepSource;

	Animator anim;
	float animSpeed= 0;

	
	Ray rf;
	Ray rb;
	public bool isDown = true;
	public bool wasDown = true;
	float speed = 65;

	const float jumpSpeed = 60;
	float gravity = 200f;

	public Quaternion home;
	public Vector3 armVec;
	float maxSpeed = 35f;
	float slowDownSpeed = 60;
	
	// Use this for initialization
	void Start () {
		anim= GetComponent<Animator>();
		anim.SetLayerWeight(1,1);
	}
	
	// Update is called once per frame
	void Update () {
		
		rf = new Ray(frontRay.transform.position,Vector3.down);
		rb = new Ray(backRay.transform.position,Vector3.down);
		
		armVec = larm.transform.rotation.eulerAngles;
		isDown = (Physics.Raycast(rf,1.5f));
		isDown = isDown || Physics.Raycast(rb,1.5f);
		
		if(isDown && !wasDown){
			//StartCoroutine( playSound(landing));
		}else if(wasDown && !isDown){
			StartCoroutine( playSound(jump));
		}
		wasDown = isDown;
		
		
		//Arm movement
		/*
		Vector3 mousePos = Input.mousePosition;
		mousePos.z = -(rarm.transform.position.z - Camera.mainCamera.transform.position.z);
 
		Vector3 worldPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);
 
		larm.transform.rotation = Quaternion.Euler(0,180,-Vector3.Angle(rarm.transform.position,worldPos));
		rarm.transform.Rotate(Vector3.back,-Input.GetAxis("Mouse X") * 10);
		
		//transform.LookAt(worldPos);
		*/
		larm.transform.Rotate(Vector3.back,-Input.GetAxis("Mouse X") * 10);
		rarm.transform.Rotate(Vector3.back,-Input.GetAxis("Mouse X") * 10);
		
		//WASD Controls
		
		bool isLeft = Input.GetKey(KeyCode.A);
		bool isRight = Input.GetKey(KeyCode.D);
		
		if(Input.GetKeyDown(KeyCode.Space) && isDown){
			rigidbody.AddForce( new Vector3( 0, jumpSpeed, 0 ), ForceMode.Impulse );
		}
		
		// gravity
		if(!isDown){
			rigidbody.AddForce( (Vector3.down * gravity * Time.deltaTime), ForceMode.VelocityChange );
		}
		
		// left force
		if(isLeft && rigidbody.velocity.x > -maxSpeed ) { // && isDown){
			rigidbody.AddForce(Vector3.left * speed * Time.deltaTime,ForceMode.VelocityChange);
			animSpeed= Mathf.Lerp(animSpeed, 10, Time.deltaTime);
			anim.SetFloat("Speed", animSpeed);
		}
		
		// right force
		if(isRight && rigidbody.velocity.x < maxSpeed) { // && isDown){
			
			rigidbody.AddForce(Vector3.right * speed * Time.deltaTime,ForceMode.VelocityChange);
			animSpeed= Mathf.Lerp(animSpeed, 10, Time.deltaTime);
			anim.SetFloat("Speed", animSpeed);
			/*
			animSpeed= Mathf.Lerp(animSpeed, 10, Time.deltaTime);
			anim.SetFloat("Speed", animSpeed);
			Debug.Log(Vector3.right * speed * Time.deltaTime);
			*/
		}
		/*
		if (!Input.GetKey(KeyCode.D)) {
			animSpeed= Mathf.Lerp(animSpeed, 0, Time.deltaTime);	
			anim.SetFloat("Speed", animSpeed);
		}
		*/
		
		//sound
		if(Mathf.Abs(gameObject.rigidbody.velocity.x) < 200){
			StartCoroutine(playStep());	
		}
		
		if (!isLeft && !isRight) {
			Vector3 pV = rigidbody.velocity;
			if (pV.x > 0) {
				float amt = slowDownSpeed * Time.deltaTime;
				if (pV.x > amt) {
					rigidbody.AddForce( Vector3.left * amt, ForceMode.Impulse );
				}
					
			}
			else if (pV.x < 0) {
				float amt = slowDownSpeed * Time.deltaTime;
				if (-pV.x > amt) {
					rigidbody.AddForce( Vector3.right * amt, ForceMode.Impulse );
				}
			}
		}

		if (isLeft) {
			animSpeed= Mathf.Lerp(animSpeed, -10, Time.deltaTime * 2);	
			anim.SetFloat("Speed", animSpeed);	
		}
		else if (isRight) {
			animSpeed= Mathf.Lerp(animSpeed, 10, Time.deltaTime * 2);	
			anim.SetFloat("Speed", animSpeed);
		}
		else {
			animSpeed= Mathf.Lerp(animSpeed, 0, Time.deltaTime * 3);	
			anim.SetFloat("Speed", animSpeed);	
		}
		
		// TODO remove
		// hack z back
		Vector3 spot = this.transform.position;
		if (spot.z != GenerateObjects.playerStartZ) {
			spot.z = GenerateObjects.playerStartZ;
			this.transform.position = spot;
		}
	}
	public IEnumerator playSound(AudioClip a){
		audio.clip = a;
		audio.Play();
		Debug.Log(a.name);
		yield return new WaitForSeconds(1);
		
	}
	public IEnumerator playStep(){
		stepSource.audio.clip = footstep;
		stepSource.audio.volume = 100;
		stepSource.audio.Play();
		yield return new WaitForSeconds(0.4f);
	}
}
